<?php
/**
 * 我的消息模块
 * User: zhc
 * Date: 2018/11/1 0001
 * Time: 21:35
 */

namespace app\index\controller;
use  library\Validator;
use module\MessageApi;
use think\Db;

class Message extends  CommonController
{

     /**
      *  我的消息列表
      */
    public  function  getMessList(){

        list($type) = Validator::make($this->request->request(), [
            'type' => 'required', //1 系统消息  2通知消息
        ]);
       $list=MessageApi::getMessagelist($this->uid,$type);
       return $this->response->api($list);
    }
    /**
     *  消息详情
     */
    public  function  getMessInfo(){

        list($mn_id) = Validator::make($this->request->request(), [
            'mn_id' => 'required', //1 系统消息  2通知消息
        ]);
        $list=MessageApi::getMessageInfo($mn_id);
        return $this->response->api($list);
    }
    /**
     * 更新消息已读状态
     */
    public function upMessageStatus(){
        list($ids) = Validator::make($this->request->request(),[
            'ids' => 'regex:/^\d+(\,\d+)*$/',
        ]);
        $re=MessageApi::readMessage($this->uid,$ids);
        return $this->response->api([],self::SUCCESS_CODE,'更新成功');
    }


    public function search_msg()
    {
        list($keyword) = Validator::make($this->request->request(), [
            'keyword' => 'required', //关键字查询
        ]);
        $where = [
            'ui_id'=>$this->uid,
            'mn_title'=>['like',"%{$keyword}%"]
        ];
        $list = Db::table('message_notice')->where($where)->select();
        return $this->response->api($list,self::SUCCESS_CODE,'成功');
    }

    public function del_msg()
    {
        list($mn_id) = Validator::make($this->request->request(), [
            'mn_id' => 'required',
        ]);
        $re = MessageApi::deleteMess(['mn_id'=>$mn_id]);
        return $this->response->api([],self::SUCCESS_CODE,'成功');
    }


    public function check_msg()
    {
        $id = $this->uid;
        $where = [
            'ui_id'=>$id,
            'mn_status'=>1
        ];
        $res = Db::table('message_notice')->where($where)->select();
        return $this->response->api(['data'=>$res,'count'=>count($res)],self::SUCCESS_CODE,'成功');
    }
}